<template>
	<view class="container">
		<view class="homeBg"></view>
		<view class="topView">
			<image src="../../static/home/homeLogo.png" mode="widthFix" class="homeLogo"></image>
<!-- 			<view class="right-Cont">
				<image src="../../static/home/customer.png" mode="widthFix" class="img"></image>
				<image src="../../static/home/msg.png" mode="widthFix" class="img"></image>
			</view> -->
		</view>

		<view class="goods_list">
			<swiper class="swiper" :interval="2000" :duration="500" :autoplay="false" @change="swiperChange"
				:current="swiperCurrent">
				<swiper-item class="swiper-item" v-for="(val,i) in tabBoxList" :key="i">
					<!-- <goods_list_item :list="val.list" @changeStatus="changeStatus" :isEnd="isEnd" v-if="swiperCurrent == 0" /> -->
					<view class="tabBox">
						<view>
							<u-tabs activeStyle="color:#fff;font-size: 32upx;font-weight: bold;"
								:current="swiperCurrent"
								inactiveStyle="color: #fff;transition: all 0.5s;font-size: 26upx;" lineColor="#FFFFFF"
								lineWidth="40upx"
								itemStyle="height:72upx;flex:1;display:flex;align-items: center;justify-content: center;"
								:list="tabBoxList" @change="tabsChange"></u-tabs>
						</view>
					</view>
					<view class="bannerBox">
						<u-swiper :list="banerList" keyName="imgs" height="320upx" radius="16upx" :interval="4000"
							@change="e => current = e.current" @tap="bannerClick">
						</u-swiper>
						<view class="indicatorS">
							<view class="indicator__dot" v-for="(item, index) in banerList" :key="index"
								:class="[index === current && 'indicator__dot--active']"></view>
						</view>
					</view>
					<scroll-view class="listview"
						:upper-threshold="1" enableBackToTop="true" scroll-y @scrolltolower="scrolltolower()">
						<news_content :list="val.list" :isEnd="isEnd" />
						<view v-show="val.list.length == 0" class="noListBox">
							<view class="noData">
								<image class="noDataImg" src="../../static/home/noData.png" mode=""></image>
								<view class="text">暂无数据</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>

		<view class="popupBox" v-if="!hasLogin && isPopupShow">
			<view v-if="!hasLogin">
				<text>请先前往登录吧！</text>
				<view class="btns">
					<navigator class="navigator" url="/pages/login/login">去登录</navigator>
					<u-icon @tap="isPopupShow = false" name="close" color="#191919" size="16"></u-icon>
				</view>
			</view>
			<view v-if="hasLogin && uerInfo.isRealName != 1">
				<text>实名认证后才可以购买哦！</text>
				<view class="btns">
					<navigator class="navigator" url="/pages/login/login">去认证</navigator>
					<u-icon @tap="isPopupShow = false" name="close" color="#191919" size="16"></u-icon>
				</view>
			</view>
		</view>
		<mix-loading ref="min_loading"></mix-loading>
	</view>
</template>

<script>
	import news_content from "./components/news_content.vue"

	import {
		PATH_IMG,
		BASE_API
	} from '@/common/config.js';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		getBannerList,
		getlistArticle
	} from '@/api/home.js';
	
	export default {
		components: {
			news_content
		},
		data() {
			return {
				isScrollY: false,
				tabBoxList: [{
						id: 1,
						name: "最新公告",
						list: [],
						key: "593465bd97454eb8b150c5d6f1a9a6ab"
					},
					{
						id: 2,
						name: "推荐新闻",
						list: [],
						key: "942dd505d92249fba0aef5c6f159e809"
					},
					{
						id: 3,
						name: "业务规则",
						list: [],
						key: "0f12844571774268ab983481dc4f9f3e"
					},
				],
				isPopupShow: true,
				tabBoxIndex: 0,
				isEnd: false,
				stock: '',
				current: 0,
				listQuery: {
					page: 1,
					pageSize: 10,
					pages: 1,
					categoryKey: "593465bd97454eb8b150c5d6f1a9a6ab",
					labelName: "",
					keyword: ""
				},
				banerList: [
					{
						imgs:require("@/static/common/test1.png")
					},
					{
						imgs:require("@/static/common/test2.png")
					}
				],
				swiperCurrent: 0,
			}
		},
		onLoad() {
			this.getBanner();
			this.getNewsList();
		},
		onShow() {
			if (!this.hasLogin) {
				uni.reLaunch({
					url: "/pages/login/login"
				});
			}
		},
		computed: {
			...mapState(["hasLogin", "uerInfo", "h5Data"])
		},
		methods: {
			bannerClick(i) {
				let obj = this.banerList[i];
				if (obj.linkUrl) {
					// uni.navigateTo({
					// 	url:obj.linkUrl
					// });
					window.location.href = obj.linkUrl;
				}
			},
			// tabs通知swiper切换
			tabsChange(e) {
				this.swiperCurrent = e.index;
				this.listQuery.page = 1;
				this.listQuery.pages = 1;
				this.isEnd = false;
				this.getNewsList();
			},
			swiperChange(e) {
				let current = e.detail.current;
				this.isEnd = false;
				if (this.swiperCurrent != current) {
					this.listQuery.page = 1;
					this.listQuery.pages = 1;
					this.swiperCurrent = current;
					this.getNewsList();
				}
			},
			getBanner() { //banner
				getBannerList({
					position: 1
				}).then(res => {
					if (res.data.success) {
						this.banerList = res.data.obj.map((im) => {
							im.imgs = this.pathImg + im.imgs;
							return im;
						});
					}
				}).catch(err => {

				});
			},
			getNewsList() { //新闻
				this.listQuery.categoryKey = this.tabBoxList[this.swiperCurrent].key;
				this.$nextTick(() => {
					this.$refs.min_loading.open('加载中，请稍后！');
				});
				if(this.listQuery.page==1){
					this.tabBoxList[this.swiperCurrent].list=[];
				}
				getlistArticle(this.listQuery).then(res => {
					if (res.data.rows) {
						let obj = res.data.rows,
							goodsList = [];
						for (let key in obj) {
							goodsList.push(obj[key]);
						}
						if (goodsList.length == 0) {
							return;
						}
						if (goodsList.length <= 10) {
							this.isEnd = true;
						}
						let newList = goodsList.filter((item, index) => {
							if (item.logoImage) {
								item.logoImage = this.pathImg + item.logoImage;
							}
							// if(item.metafile){
							// 	item.metafile = JSON.parse(item.metafile);
							// }
							return item
						});
						this.tabBoxList[this.swiperCurrent].list = [...this.tabBoxList[this.swiperCurrent].list,...newList];
						this.listQuery.pages = res.data.totalPage;
					}
					uni.stopPullDownRefresh();
				}).catch(err => {
					uni.stopPullDownRefresh();
				}).finally(() => {
					uni.stopPullDownRefresh();
					this.$refs.min_loading.close();
					console.log(this.tabBoxList[this.swiperCurrent].list.length);
					console.log(this.listQuery)
				});
			},
			//上拉数据刷新
			scrolltolower() {
				if (this.listQuery.pages > this.listQuery.page) {
					this.listQuery.page++;
					this.getNewsList();
				} else {
					this.isEnd = true;
				}
			},
		},
		onPullDownRefresh() { //下拉刷新
		    this.listQuery.pages=1;
			this.listQuery.page = 1;
			this.getNewsList();
		},
		onReachBottom() {
			this.isScrollY = true;
		},
	}
</script>
<style scoped lang="less">
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		padding-top: 102upx;

		.homeBg {
			width: 100%;
			height: 668upx;
			background: url(../../static/login/bg.png) no-repeat top center;
			background-size: 100% 100%;
			position: fixed;
			top: 0;
			left: 0;
		}

		.topView {
			width: 100%;
			padding: 0 42upx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			position: relative;

			.homeLogo {
				width: 108upx;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
			}

			.right-Cont {
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.img {
					width: 44upx;
					margin-left: 40upx;
				}
			}
		}

		.logoBox {
			width: 100%;
			height: 100%;

			.navBox {
				height: 216upx;
				background-image: url("@/static/common/bg1.png");
				background-size: 100% 100%;
				background-repeat: no-repeat;
				padding: 0 52upx;
				padding-top: 52upx;
				text-align: center;

				.names {
					width: 100%;
				}

				.u-nav-left {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					margin-bottom: 16upx;

					.title {
						flex: 1%;
						max-width: 272upx;
						margin-left: 28upx;
					}

					.logo {
						width: 60upx;
					}

					.name {
						display: flex;
						flex-wrap: wrap;
						padding-left: 14upx;
						color: #FFF;
						width: 190upx;
						text-align: left;

						.chinese {
							width: 100%;
							margin-bottom: 6upx;
						}

						.english {
							width: 100%;
						}

						image {
							vertical-align: top;
							width: 100%;
						}
					}
				}
			}
		}

		.popupBox {
			width: 100%;
			height: 84upx;
			position: fixed;
			bottom: 118upx;
			left: 0;
			padding: 0 40upx;
			z-index: 99;

			>view {
				width: 100%;
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #FCCC35;
				border-radius: 10upx;
				font-size: 24upx;
				padding: 0 30upx;
				color: #151515;

				.btns {
					display: flex;
					align-items: center;
					font-size: 0;

					.navigator {
						width: 130upx;
						height: 55upx;
						background: #191919;
						border-radius: 28upx;
						font-size: 24upx;
						font-weight: 400;
						color: #FCCC35;
						line-height: 55upx;
						text-align: center;
						margin-right: 10upx;
					}
				}
			}
		}

		.tabBox {
			width: 100%;
			margin-bottom: 40upx;
			margin-top: 32upx;
			padding: 0 62upx;
		}

		.bannerBox {
			padding: 0 30upx;
			// padding-top: 50upx;
			position: relative;
			margin-bottom: 50upx;

			.indicatorS {
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 14upx;
				position: absolute;
				bottom: 20upx;
				left: 0;

				.indicator__dot {
					height: 100%;
					width: 14upx;
					border-radius: 50%;
					background: rgba(206, 165, 57, .5);
					margin: 0 10upx;
					transition: background-color 0.3s;
				}

				.indicator__dot--active {
					background-color: #CEA539;
				}
			}
		}
		
		.listview{
			width: 100%;
			height: calc(100% - 400upx);
			padding-bottom: 40upx;
		}

		.goods_list {
			width: 100%;
			height: calc(100% - 30upx);

			.swiper {
				width: 100%;
				height: 100%;

				.swiper-item {
					width: 100%;
					height: 100%;
					padding-bottom: 40upx;
					position: relative;

					.listCentent {
						padding-bottom: 200upx;
						height: 100%;
					}
				}
			}

		}

		.noListBox {
			text-align: center;

			.noData {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-direction: column;
				margin-top: 200upx;

				.noDataImg {
					width: 316upx;
					height: 126upx;
				}

				.text {
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #B0B5BC;
					line-height: 92upx;
				}
			}
		}
	}
</style>
